package com.kerbores.utils.velocity;

import java.util.Calendar;
import java.util.Date;

import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.config.InvalidScope;

import com.kerbores.utils.common.DateUtils;

/**
 * @author Kerbores<br>
 *
 *         create at 2014年11月9日 下午10:50:10
 */
@DefaultKey("T")
@InvalidScope({ "application" })
public class T extends DateUtils {
	protected static final String[] _MMM = new String[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };

	public static String calc(Date date) {
		return formatForRead(date);
	}

	public static String formatForRead(Date d) {
		long ms = System.currentTimeMillis() - d.getTime();
		if (ms < (60000)) {
			return "刚刚";
		}
		if (ms < (60 * 60000)) {
			return "" + (ms / 60000) + "分钟前";
		}

		if (ms < (24 * 3600 * 1000)) {
			return "" + (ms / 3600000) + "小时前";
		}

		if (ms < (7 * 24 * 3600 * 1000)) {
			return "" + (ms / (24 * 3600000)) + "天前";
		}

		Calendar c = Calendar.getInstance();
		int thisYear = c.get(Calendar.YEAR);

		c.setTime(d);
		int yy = c.get(Calendar.YEAR);
		if (thisYear == yy) {
			return format("MM月dd日", d);
		}
		return format("yy年MM月", d);
	}

}
